package cn.com.jslh.boarpro.main.view.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mindorks.nybus.NYBus;
import com.mindorks.nybus.annotation.Subscribe;
import com.mindorks.nybus.event.Channel;

import org.json.JSONArray;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.BaseFragment;
import cn.com.jslh.boarpro.main.contract.ShowPigInfoContract;
import cn.com.jslh.boarpro.main.presenter.ShowPigInfoPresenterImpl;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShowPigInfoFragment extends BaseFragment implements ShowPigInfoContract.View {


    @BindView(R.id.inforecycleview)
    RecyclerView inforecycleview;
    private ShowPigInfoContract.Presenter presenter;

    public ShowPigInfoFragment() {
        // Required empty public constructor
        new ShowPigInfoPresenterImpl(getContext(), this);
        NYBus.get().register(this, Channel.ONE);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_show_pig_info, container, false);
        unbinder = ButterKnife.bind(this, view);
        // Inflate the layout for this fragment
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        inforecycleview.setLayoutManager(linearLayoutManager);
        inforecycleview.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        presenter = new ShowPigInfoPresenterImpl(getContext(),this);
        presenter.bindRV(inforecycleview);
        presenter.start();
        return view;
    }

    @Override
    public void setPresenter(ShowPigInfoContract.Presenter presenter) {
        this.presenter = presenter;
    }

    public static void initInfoShowFragment(FragmentManager fm) {
        FragmentTransaction tx = fm.beginTransaction();
        tx.add(R.id.fragment_layout, new ShowPigInfoFragment());
        tx.commit();
    }

    public interface InfoInterface {
        void success(String value,String id);

        void faild(String msg);
    }

    public static class InfoEvent {
        JSONArray datas;

        public InfoEvent(JSONArray datas) {
            this.datas = datas;
        }

    }

    @Subscribe(channelId = Channel.ONE)
    public void onEvent(JSONArray datas) {
        presenter.getPigInfo(datas);
    }

    public static void postEvent(JSONArray datas) {
        NYBus.get().post(datas,Channel.ONE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        NYBus.get().unregister(this, Channel.ONE);
    }
}
